package org.tio.im.client;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.tio.core.exception.AioDecodeException;
import org.tio.im.common.Const;
import org.tio.im.common.ImStatus;
import org.tio.im.common.Protocol;
import org.tio.im.common.packets.ChatBody;
import org.tio.im.common.packets.LoginReqBody;
import org.tio.im.common.session.id.impl.UUIDSessionIdGenerator;
import org.tio.utils.thread.pool.DefaultThreadFactory;

import com.alibaba.fastjson.JSON;
public class AndroidProtocalTest {
	private Socket socket;
 	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AndroidProtocalTest coxy =  new AndroidProtocalTest();
			coxy.send();  
	/*	ChatBody chatBody = new ChatBody()
				.setFrom("hello_client")
				.setTo("admin")
				.setMsgType("text")
				.setChatType(1)
				.setGroup_id("100")
				.setContent("普通Socket客户端转Ws消息测试!");
		chatBody.setCmd(11);
			byte[] pkgBody  = encode(chatBody.toByte()) ;*/	
			String userName = "6gWtP3REJwIUcGtv9F7IsW/SrD089PEHTx+sUdkLYISjXbHzudBRLCfttycYHJgTYt5QHhyidUfIYEtXdrrovA==";
			String password = "wv83u6jsRrHrx1gGeR/xHDmcRA6SGohrH2vJkO97Hz/3N6GhwWLFdzvkVMqnUittuUHheWDiPcLd/Z419LE9OStPyBMACPYx77yjUGajqI0=";
			LoginReqBody loginReqBody = new LoginReqBody(userName,password);
	
			//心跳机智 
			
			
	}

	public AndroidProtocalTest(){
		try{
			//socket = new Socket("192.168.1.135",Const.SERVER_PORT);
			socket = new Socket("120.78.77.215",39683);
		}catch(Exception e){
			e.printStackTrace();
		}
	} 
	static int HEADER_LENGHT =7;
	
	private static ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
	public static byte[] encode(byte[] body,byte cmdByte)
	{
		int bodyLen = 0;
		if (body != null)
		{
			bodyLen = body.length;
		} 
		//bytebuffer的总长度是 = 消息头的长度 + 消息体的长度
		int allLen = HEADER_LENGHT + bodyLen;
		//创建一个新的bytebuffer
		ByteBuffer buffer = ByteBuffer.allocate(allLen);
		//设置字节序
		buffer.order(byteOrder);
		byte versionByte = 1;	
		byte marskByte = 0;
		buffer.put(versionByte);
		buffer.put(marskByte);
		//byte cmdByte = 11;		
		//byte cmdByte = 11;		
		buffer.put(cmdByte);

		//写入消息头----消息头的内容就是消息体的长度
		buffer.putInt(bodyLen);	
		//写入消息体
		if (body != null)
		{
			buffer.put(body);
		}
		return buffer.array();
	}
	
	private static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
	public static final long KEEP_ALIVE_TIME = 90L;
	private static final int MAX_POOL_SIZE = CORE_POOL_SIZE * 4 < 256 ? 256 : CORE_POOL_SIZE * 4;	
	private final static Object syncLock = new Object();
	// 线程池
	static ThreadPoolExecutor groupExecutor = null;
	/**
	 * 获取线程池对象
	 * @return
	 */
	public static ThreadPoolExecutor getExecutor() {
		if (groupExecutor == null) {
			synchronized (syncLock) {
				if (groupExecutor == null) {
					LinkedBlockingQueue<Runnable> groupQueue = new LinkedBlockingQueue<>();
					String groupThreadName = "social-group";
					groupExecutor = new ThreadPoolExecutor(MAX_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, groupQueue,
							DefaultThreadFactory.getInstance(groupThreadName, Thread.NORM_PRIORITY));
					groupExecutor.prestartAllCoreThreads();
				}
			}
		}
		return groupExecutor;
	}
	public void send(){ 
		try{ 
		/*	ChatBody chatBody = new ChatBody()
					.setFrom("hello_client")
					.setTo("admin")
					.setMsgType("text")
					.setChatType(1)
					.setGroup_id("100")
					.setContent("普通Socket客户端转Ws消息测试!");
			chatBody.setCmd(11);*/
			
			/*String userName = "YDmW7YgQ5bTqtH5iwaGo6nSDSZ1ESPugZFvDrgYFCKYCtaw1jF3mn9KAbqM26N7GoD5ifnpQ1HQ0g41q4eHHKQ==";
			String password = "zPbhbV6P8NouCQrqZIXElq2paz2/RT2b2V0xb9wEk34B/BXszt7pDljv4eFfrlYO0PIktmW9SrfjPomebgFFAQ==";
			*/
			/*String userName = "Rc8UthsNr21J+awy/QqE7SiAZJqdRBmqckBJY3qvUhLF2KsEdcdoPcKvYuEx/VXMzOk+98iXFBJ01rLQdAWzng==";
			String password = "zPbhbV6P8NouCQrqZIXElq2paz2/RT2b2V0xb9wEk34B/BXszt7pDljv4eFfrlYO0PIktmW9SrfjPomebgFFAQ==";
			*/
		/*	String userName = "Rc8UthsNr21J+awy/QqE7SiAZJqdRBmqckBJY3qvUhKq6p2jHf2G39GJRSig/vPUyc4kFpl2JzUg8g5ZWmi5Hw==";
			String password = "zPbhbV6P8NouCQrqZIXElq2paz2/RT2b2V0xb9wEk34B/BXszt7pDljv4eFfrlYO0PIktmW9SrfjPomebgFFAQ==";
			*/
			
			String token = "mini:2b31c1bcbc2f40fca1b2f074835cf961" + "_" + System.currentTimeMillis();
			//String token = "login:178cb14d1d43448e8e688076b5db19ce" + "_" + System.currentTimeMillis();
			//header_token判断用户是否登录
			String loginname = AESCipher.aesCommEncrypt(token);

			Map<String,Object> map = new HashMap<String, Object>();
			map.put("shopId", "938740497175040299");
			map.put("shopName", "测试商店");
			//房间信息
			String password = AESCipher.aesWxCommEncrypt(JSON.toJSONString(map));
			
			LoginReqBody chatBody = new LoginReqBody(loginname,password);

			//byte[] pkgBody = encode(chatBody.toByte(),11) ;			
		/*	int bodyLen = 0;
			if (body != null)
			{
				bodyLen = body.length;
			} 
			int allLen = HEADER_LENGHT + bodyLen; 
			byte[] pkgBody = new byte[allLen];
			
			byte[] versionByte = new byte[] {1};
			System.arraycopy(versionByte,0, pkgBody, 0, 1);
			
			byte[] cmdByte = new byte[] {11};
			System.arraycopy(cmdByte,0, pkgBody, 1, 1);

			System.arraycopy(intToByteArray(body.length),0, pkgBody, 2, 4);

			System.arraycopy(body,0, pkgBody, 6, bodyLen);*/
			
			 OutputStream oos = socket.getOutputStream() ; 
			 
				chatBody.setId(UUIDSessionIdGenerator.instance.sessionId(null));
				byte cmdByte = 5;
				
				for (int i = 0; i < 1; i++) {
					chatBody.setId(System.currentTimeMillis() + "");
					byte[] pkgBody  = encode(chatBody.toByte(),cmdByte) ;		
					
					oos.write(pkgBody); 
					oos.flush(); 
				}

				
				
				while(true){
					Thread.sleep(500);
					InputStream ret=socket.getInputStream(); 
					try {
					 byte[] versionLen = new byte[1];
					 byte[] maskLen = new byte[1];
					 byte[] cmdLen = new byte[1];
				
				     //加入报文头部分  前四位
					 byte[] resLength = new byte[4];
					 ret.read(versionLen);
					 ret.read(maskLen);
					 ret.read(cmdLen);
					 ret.read(resLength);
					  int  resLengthInt=byteArrayToInt(resLength,0);
						  System.out.println("cmd:"+cmdLen[0]+",resLength:"+resLengthInt);
						 
							byte[] data = new byte[resLengthInt];
							
							ret.read(data);
							
							
							
							
							StringBuffer sb = new StringBuffer();
							for (byte b : data) {
								if(b > 0) {
									sb.append(b).append(",");
								}
							}
							System.out.println("数据长度:resLengthInt:"+resLengthInt+",datalen:"+sb.length()+",content："+sb);
							String content = new String(data);
							System.out.println(content);
					} catch (Exception e) {
						
						
						e.printStackTrace();
					}finally {
					
					}

				}

			 
			 
		/*		try {
					chatBody.setId(UUIDSessionIdGenerator.instance.sessionId(null));
					byte[] pkgBody  = encode(chatBody.toByte()) ;		
					
					oos.write(pkgBody); 
					oos.flush(); 
					InputStream ret=socket.getInputStream(); 
					byte[] data = new byte[1024];
					int len = 0;// 该值用于计算当前实际下载了多少字节
					while ((len = ret.read(data, 0, data.length)) != -1) {
						String temp = new String(data);
						 data = new byte[1024];
						System.out.println(temp);
						if (temp.contains("######")) {
							break;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}*/
			 
/*			for (int i = 0; i < 1; i++) {
				getExecutor().execute(new Runnable() {
					@Override
					public void run() {
						try {
							chatBody.setId(UUIDSessionIdGenerator.instance.sessionId(null));
							byte[] pkgBody  = encode(chatBody.toByte()) ;		
							
							oos.write(pkgBody); 
							oos.flush(); 
							
							InputStream ret=socket.getInputStream(); 
						     //加入报文头部分  前四位
							 byte[] resLength = new byte[6];
							 ret.read(resLength);
							  int  resLengthInt=byteArrayToInt(resLength,0);
							 
								byte[] data = new byte[resLengthInt];
								
								ret.read(data);
				 
								System.out.println(new String(data));
						} catch (Exception e) {
							e.printStackTrace();
						}

						
					}
				});
				


			}*/


				
		//	ret.close();
		//	oos.close();
			
			//socket.close();
		}catch(Exception e){
			e.printStackTrace();
		} 
	}
	

	
	
	
	public static int byteArrayToInt(byte[] b, int offset) {
	       int value= 0;
	       for (int i = 0; i < 4; i++) {
	           int shift= (4 - 1 - i) * 8;
	           value +=(b[i + offset] & 0x000000FF) << shift;
	       }
	       return value;
	 }
	public static byte[] intToByteArray(int i) throws Exception {
		  ByteArrayOutputStream buf = new ByteArrayOutputStream();   
		  DataOutputStream out = new DataOutputStream(buf);   
		  out.writeInt(i);   
		  byte[] b = buf.toByteArray();
		  out.close();
		  buf.close();
		  return b;
		 }
	public static byte[] byteToByteArray(byte i) throws Exception {
		  ByteArrayOutputStream buf = new ByteArrayOutputStream();   
		  DataOutputStream out = new DataOutputStream(buf);   
		  out.writeByte(i);   
		  byte[] b = buf.toByteArray();
		  out.close();
		  buf.close();
		  return b;
		 }
}
